package record

import (
	"time"

	confv1beta1 "gopkg.openfuyao.cn/bkecommon/cluster/api/v1beta1"
	"sigs.k8s.io/controller-runtime/pkg/client"

	bkev1beta1 "gopkg.openfuyao.cn/cluster-api-provider-bke/api/v1beta1"
	bkemetrics "gopkg.openfuyao.cn/cluster-api-provider-bke/pkg/metrics"
	"gopkg.openfuyao.cn/cluster-api-provider-bke/utils"
	"gopkg.openfuyao.cn/cluster-api-provider-bke/utils/config"
)

func ClusterHealthyCountRecord(obj client.Object, status confv1beta1.ClusterStatus) {
	if config.MetricsAddr == "0" {
		return
	}
	switch status {
	case bkev1beta1.ClusterReady:
		bkemetrics.MetricRegister.RecordGaugeVec(
			bkemetrics.ClusterReadyStatus,
			utils.ClientObjNS(obj),
		).Set(1)

	case bkev1beta1.ClusterUnhealthy:
		bkemetrics.MetricRegister.RecordCounterVec(
			bkemetrics.ClusterUnhealthyCount,
			utils.ClientObjNS(obj),
		).Inc()

		bkemetrics.MetricRegister.RecordGaugeVec(
			bkemetrics.ClusterReadyStatus,
			utils.ClientObjNS(obj),
			//bkemetrics.TimeNow(),
		).Set(0)
	}
}

func ClusterBootstrapDurationRecord(obj client.Object) {
	if config.MetricsAddr == "0" {
		return
	}

	startTime := obj.GetCreationTimestamp().Time

	bkemetrics.MetricRegister.RecordGaugeVec(
		bkemetrics.ClusterBootstrapDurationSeconds,
		utils.ClientObjNS(obj),
		bkemetrics.FormatTime(startTime),
		bkemetrics.TimeNow(),
	).Set(time.Since(startTime).Seconds())

}
